// Function on the checkbox to display or not : france 
function displayFranceGraphDeltaPm() {
	franceGraphDelta = !franceGraphDelta;
	loadGraphDeltaPm(globalNivGeog, globalGeog, idGeog);
}

// Function on the checkbox to display or not : zone 
function displayZoneGraphDeltaPm() {
	zoneGraphDelta = !zoneGraphDelta;
	loadGraphDeltaPm(globalNivGeog, globalGeog, idGeog);
}

// Function on the checkbox to display or not : region 
function displayRegionGraphDeltaPm() {
	regionGraphDelta = !regionGraphDelta;
	loadGraphDeltaPm(globalNivGeog, globalGeog, idGeog);
}

// Mise à jour des informations de la zone sud 
function updateLevelInformations() {
	var source		= $('#sourceId').get(0).value;
	var periode		= $('#periodId').get(0).value;
	var unite		= $('#unitId').get(0).value;
	$.ajax({ 
		traditional: true, 
		type: 'POST', 
		url: './updateLevelInformations.do', 
		target: "_blank", 
		data : {
			idGeog:		idGeog,
			nivGeog: 	globalNivGeog,
			source: source,
			unite:		unite,
			periode:	periode
		}, 
		error: function() {},
		success: function(result) { 
			$('#infoSudTypeNiveau').html(globalNivGeog + " :");
			$('#infoSudNiveau').html(result.name);
			$('#infoSudPM').html(result.pm);
			$('#infoSudDeltaPM').html(result.deltapm);
			// Rafraichissement de la zone sud 
			var southZone = Ext.getCmp('southZone');
			if (!southZone.hidden) {
				var grid2 = Ext.getCmp('grid2');
				if (!grid2.hidden) {
					$("#grid").html('<img src="img/ajax-loader.gif" style="padding-right:5px;" />');
					if (!ugp) {
						loadGridUGA("");
					} else {
						loadGrid("");
					}
				}
				var grid3 = Ext.getCmp('grid3');
				if (!grid3.hidden) {
					$("#gridDoctors").html('<img src="img/ajax-loader.gif" style="padding-right:5px;" />');
					loadGridDoctors();
				}
			} 
		}
	});
}


/**
 * Fonctions pour telecharger les fichiers XLS et PDF.
 */
function downloadExcelTopUgpn() {
	downloadDocuments("exportGridExcel");
}
function downloadPdfTopUgpn() {
	downloadDocuments("exportGridPdf");
}
function downloadExcelTopUga() {
	downloadDocuments("exportGridExcelUGA");
}
function downloadPdfTopUga() {
	downloadDocuments("exportGridPdfUGA");
}
function downloadExcelDoctors() {
	downloadDocuments("exportGridDoctorsExcel");
}
function downloadPdfDoctors() {
	downloadDocuments("exportGridDoctorsPdf");
}
function downloadExcelDoctorsUga() {
	downloadDocuments("exportGridDoctorsExcelUga");
}
function downloadPdfDoctorsUga() {
	downloadDocuments("exportGridDoctorsPdfUga");
}
function downloadExcelMedecins() {
	downloadDocumentsMedecins("exportGridMedecinsExcel");
}
function downloadPdfMedecins() {
	downloadDocumentsMedecins("exportGridMedecinsPdf");
}
function downloadDocuments(action) {
	resizeWest = false;
	resizeEast = false;
	var product    = $('#productId').get(0).value;
	var source = encodeURIComponent($('#sourceId').get(0).value); 
	var periode = $('#periodId').get(0).value;
	var unite = $('#unitId').get(0).value;
	var cs_division		= $('#activite-equipe-id').get(0).value; 
	var periodeActivite = $('#activite-periode-id').get(0).value;
	var specialite		= $('#activite-specialite-id').get(0).value;
	window.open("./" + action + ".do?product=" + product + "&source=" + source + "&codeUga=" + globalCodeUGP + "&codeUgp=" + globalCodeUGP + "&nivGeog=" + globalNivGeog + "&geog=" + globalGeog + "&periode=" + periode + "&unite=" + unite + "&idGeog=" + idGeog+ "&cs_division=" + cs_division + "&periodeActivite=" + periodeActivite + "&specialite=" + specialite + "&segments=" + allSegments, "", "", "");
}
function downloadDocumentsMedecins(action) {
	resizeWest = false;
	resizeEast = false;
	var cs_division		= $('#activite-equipe-id').get(0).value; 
	var periodeActivite = $('#activite-periode-id').get(0).value;
	var specialite		= $('#activite-specialite-id').get(0).value;
	window.open("./" + action + ".do?nivGeog=" + globalNivGeog + "&idGeog=" + idGeog+ "&cs_division=" + cs_division + "&periodeActivite=" + periodeActivite + "&specialite=" + specialite + "&segments=" + allSegments, "", "", "");
}


/**
 * Mise à jour de la partie Informations (zone ouest).
 */
function refreshInformations() {
	$('#infoProduct').html($('#productId').get(0).options[$('#productId').get(0).selectedIndex].text);
	$('#infoPeriode').html($('#periodId').get(0).options[$('#periodId').get(0).selectedIndex].text);
	$('#infoSource').html($('#sourceId').get(0).options[$('#sourceId').get(0).selectedIndex].text);
	$('#infoUnite').html($('#unitId').get(0).options[$('#unitId').get(0).selectedIndex].text);
	$('#infoEquipe').html($('#activite-equipe-id').get(0).options[$('#activite-equipe-id').get(0).selectedIndex].text);
	$('#infoPeriodeActivite').html($('#activite-periode-id').get(0).options[$('#activite-periode-id').get(0).selectedIndex].text);
	$('#infoSpecialite').html($('#activite-specialite-id').get(0).options[$('#activite-specialite-id').get(0).selectedIndex].text);
	//$('#infoIndicateur').html($('#carto-indicateur-id').get(0).options[$('#carto-indicateur-id').get(0).selectedIndex].text);
}


/**
 * Rafraichissement du flag UGP lors du changement de source. 
 */
function refreshUGP() {
	// Execution rapide en JS, modification de l'attribut ugp.
	var source = $('#sourceId').get(0).value;
	if (parseInt(source) == 1 || parseInt(source) == 2 || parseInt(source) == 3 || parseInt(source) == 4 || parseInt(source) == 5 || parseInt(source) == 6) {
		ugp = true;
	} else {
		ugp = false;
	}
	// Sauvegarde en session de la valeur du flag ugp. 
	$.ajax({
 		traditional: true, 
 		type: 'POST', 
 		url: './refreshUGP.do',
 		target: "_blank", 
 		data : { source: source },
 		error: function () {},
 		success: function(result) {}
	});
}


/**
 * Re-initialise le mix produit.
 */
function initializeMixProduit() {
	var source = $('#sourceId').get(0).value;
	$.ajax({
 		traditional: true, 
 		type: 'POST', 
 		url: './initializeMixProduit.do',
 		target: "_blank", 
 		data : { source: source },
 		error: function () {},
 		success: function(result) { 
 			refreshCharts(); // Rafraichissement des graphes 
 			loadJsTreeProducts(); // Rafraichissement de la liste des produits 
 		}
	});
}


/**
 * Fonction permettant simuler un clic sur le niveau précédant dans l'arbre de navigation.
 */
function niveauPrecedantSecto() {
	$("#infobulleregion").hide();
	$("#infobulle").hide();
	if (globalNivGeog != 'France') {
		var elem = $("#" + idGeog + "-" + globalNivGeog).parent().parent().parent().children("a").get(0).id.split("-");
		var geogTmp = $("#" + idGeog + "-" + globalNivGeog).parent().parent().parent().children("a").get(0).textContent.replace(/\s/g, "");
		if (elem[1] != undefined) { // retour autre que niveau france 
			globalNivGeog = elem[1];
			globalGeog = geogTmp;
			idGeog = elem[0];
			$("#" + idGeog + "-" + globalNivGeog).addClass("jstree-clicked");
			$("#" + idGeog + "-" + globalNivGeog).click();
		} else { // retour niveau France
			globalNivGeog = 'France';
			globalGeog = geogTmp;
			idGeog = 0;
			$("#France").addClass("jstree-clicked");
			$("#France").click();
		}
	} 
}

